import 'dart:convert';

import 'package:ai_report/util/tool.dart';
import 'package:tile/tile.dart';

import 'data/terms_coordinates.dart';

List<TermsCoordinates> directionList = List.empty(growable: true);

String fileTermsCoordinates =
    'packages/ai_report/assets/terms_coordinates.json';

TermsCoordinates? calculateDirection(String s) {
  if (directionList.isEmpty) {
    _directionList(s);
  } else {
    for (var it in directionList) {
      if (it.coordinate == s) {
        return it;
      }
    }
  }
  return TermsCoordinates();
}

_directionList(String s) async {
  var content = await loadAsset(fileTermsCoordinates);
  List<dynamic> directions =
      json.decode(content).map((e) => TermsCoordinates.fromJson(e)).toList();
  directionList = List<TermsCoordinates>.from(directions);

  for (var it in directionList) {
    if (it.coordinate == s) {
      return it;
    }
  }
}

enum Direction { UP, DOWN, HORIZONTAL, OTHER }

Direction isVertical(List<ChessStep> stepList, ChessStep nextStep,
    {int distance = 1}) {
  var step = stepList[0];
  final int x = nextStep.y;
  final int y = 20 - nextStep.x;
  if (step.x == x) {
    if (step.y - y == distance) {
      return Direction.UP;
    } else if (y - step.y == distance) {
      return Direction.DOWN;
    }
  } else {
    if (step.y == nextStep.y) {
      return Direction.HORIZONTAL;
    }
  }
  return Direction.OTHER;
}
